<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\controller\Common;
use app\admin\model\AuthRule;
class Rule extends Controller
{
//*******************权限**********************
	public function index(){
		$auth_rule = new AuthRule;
		$data = $auth_rule->getTreeData('tree','id','title');
		$this->assign('data',$data);
		return view();
	}
	function form(){
		$data = input('post.');
		$id = $data['id'];
		if(empty($id)){
			$res = db('auth_rule')->insertGetId($data);
			if($res){
				/*********************记录日志***********************/
                addLog(session('user_name').'添加权限成功-'.$data['title'].' [ ID:'.$res.' ]');
				$this->success('添加成功','Rule/index');
			}else{
				$this->error('添加失败','Rule/index');
			}
		}else{
			$res = db('auth_rule')->where('id='.$id)->update($data);
			if($res !== false){
				/*********************记录日志***********************/
                addLog(session('user_name').'编辑权限成功-'.$data['title'].' [ ID:'.$id.' ]');
				$this->success('编辑成功','Rule/index');
			}else{
				$this->error('编辑失败','Rule/index');
			}
		}
	}
	function del(){
		$id = input('route.id');
		$data = db('auth_rule')->where(array('id'=>$id))->find();
		$res = db('auth_rule')->where(array('id'=>$id))->delete();
		if($res){
			$json = array('status'=>1,'msg'=>'操作成功','data'=>'Rule/index');
			/*********************记录日志***********************/
            addLog(session('user_name').'删除权限成功-'.$data['title'].' [ ID:'.$id.' ]');
		}else{
			$json = array('status'=>0,'msg'=>'操作失败','data'=>'Rule/index');
		}
		echo json_encode($json);exit;
	}
//*******************用户组**********************
	function group(){
		$data = db('auth_group')->select();
		$this->assign('data',$data);
		return view();
	}
	function groupForm(){
		$data = input('post.');
		empty($data['rules']) && $data['rules'] = '1';
		$id = $data['id'];
		if(empty($id)){
			$res = db('auth_group')->insertGetId($data);
			if($res){
				/*********************记录日志***********************/
            	addLog(session('user_name').'添加管理组成功-'.$data['title'].' [ ID:'.$res.' ]');
				$this->success('添加成功','Rule/group');
			}else{
				$this->error('添加失败','Rule/group');
			}
		}else{
			$res = db('auth_group')->where(array('id'=>$id))->update($data);
			if($res !== false){
				/*********************记录日志***********************/
            	addLog(session('user_name').'修改管理组成功-'.$data['title'].' [ ID:'.$id.' ]');
				$this->success('操作成功','Rule/group');
			}else{
				$this->error('操作失败','Rule/group');
			}
		}
	}
	function groupDel(){
		$id = input('route.id');
		$data = db('auth_group')->where(array('id'=>$id))->find();
		$res = db('auth_group')->where(array('id'=>$id))->delete();
		if($res){
			/*********************记录日志***********************/
            addLog(session('user_name').'删除管理组成功-'.$data['title'].' [ ID:'.$id.' ]');
			$json = array('status'=>1,'msg'=>'操作成功','data'=>'Rule/group');
		}else{
			$json = array('status'=>-1,'msg'=>'操作失败','data'=>'Rule/group');
		}
		echo json_encode($json);exit;
	}
//*******************权限-用户组**********************
	function ruleGroup(){
		$id = input('route.id');
		$group_data = db('auth_group')->where(array('id'=>$id))->find();
		$group_data['rules'] = explode(',',$group_data['rules']);
		$auth_rule = new AuthRule;
		$rule_data = $auth_rule->getTreeData('level','id','title');
		$assign = array(
            'group_data'=>$group_data,
            'rule_data'=>$rule_data
        );
        $this->assign($assign);
        return view('ruleGroup');
	}

	function ruleForm(){
        $data = input('post.');
        $data['rules'] = implode(',',$data['rule_ids']);
        $id = $data['id'];
        unset($data['id']);
        unset($data['rule_ids']);
        if(empty($id)){
            $res = db('auth_group')->insertGetId($data);
            if($res){
                $json = array('status' => 1,'msg'=>'添加成功','data'=>'Rule/group');
                /*********************记录日志***********************/
            	addLog(session('user_name').'分配权限成功-[ ID:'.$id.' ]');
            }else{
            	$json = array('status'=>2,'msg'=>'添加失败','data'=>'Rule/group');
            }
            echo json_encode($json);exit;
        }else{
        	$res = db('auth_group')->where(array('id'=>$id))->update($data);
        	if($res !== false){
        		$json = array('status'=>1,'msg'=>'操作成功','data'=>'Rule/group');
        		/*********************记录日志***********************/
            	addLog(session('user_name').'分配权限成功-[ ID:'.$id.' ]');
        	}else{
        		$json = array('status'=>2,'msg'=>'操作失败','data'=>'Rule/group');
        	}
        	echo json_encode($json);exit;
        }
    }






}